<template>
    <div>
        <el-dialog title="未设置客户" :visible.sync="setVisible" width="50%" class="stage_dialog">
            <div v-if="step == '1'">
                <div>
                    <el-input v-model="queryData.clientName" placeholder="客户名称" size="small"></el-input>&ensp;&ensp;
<!--                    <el-input v-model="queryData.tag" placeholder="客户标签" size="small"></el-input>-->
                    <el-button size="small" type="primary" @click="queryClientHandle(1,pagePagination.pageSize)">查询</el-button>
                </div>

                <div class="interspaceH"></div>

                <div>
                    <el-table @selection-change="tableRowChange" :data="dataSource"
                              :header-cell-style="{background:'#F3F4F7',color:'#555'}" style="width: 100%">
                        <el-table-column type="selection" width="55" align="center"></el-table-column>
                        <el-table-column prop="clientName" label="未设置客户"></el-table-column>
                        <el-table-column prop="empName" label="所属员工"></el-table-column>
                        <el-table-column prop="joinTime" label="创建时间" :formatter="formatter"></el-table-column>
                    </el-table>

                    <div class="interspaceH"></div>

                    <el-pagination @size-change="queryDataHandle(pagePagination.currentPage,$event)"
                                   @current-change="queryDataHandle($event,pagePagination.pageSize)"
                                   :current-page="pagePagination.currentPage"
                                   :page-sizes="pagePagination.pageSizes" :page-size="pagePagination.pageSize"
                                   layout="total, sizes, prev, pager, next"
                                   :total="pagePagination.total">
                    </el-pagination>
                </div>
            </div>
            <div v-else-if="step == '2'">
                <div>已选择{{ multipleSelection.length }}个客户</div>
                <span>选择客户阶段：</span>
                <el-select v-model="step2Data.clientStage" placeholder="请选择">
                    <el-option
                            v-for="item in clientStageOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button v-if="step == '1'" size="small" @click="setVisible = false">返回</el-button>
                <el-button v-if="step == '2'" size="small" @click="step = '1'">上一步</el-button>
                <el-divider direction="vertical"></el-divider>
                <el-button v-if="step == '1'" size="small" type="primary" @click="multipleSelection.length > 0 ? step = '2' : $message('请选择客户')">设置客户阶段</el-button>
                <el-button v-if="step == '2'" size="small" type="primary" @click="submitHandle">完成</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

const initQueryData = {
    clientName: '',//名称
}

export default {
    name: "client_set",
    data() {
        return {
            multipleSelection: [],
            setVisible: false,
            step: '1',
            step2Data: {
                clientStage: ''
            },
            dictionary: {},
            queryData: {...initQueryData},
            //字段转换
            fieldType: {
                joinTime: "datetime"
            },
            clientStageOptions: [],
            //列表数据
            dataSource: [],
            //分页后列表数据
            pagePagination: {
                currentPage: 1,
                pageSizes: [10, 15, 20, 25],
                pageSize: 10,
                total: 0
            },
        }
    },
    mounted() {
    },
    methods: {
        init() {
            this.queryData = {...initQueryData};
            this.queryClientHandle();
            this.queryStageHandle();
        },
        queryClientHandle(page = 1, row = 10) {
            let that = this

            let payload = {
                clientName: that.queryData.clientName,
                clientStatus: true,
                clientType: "1",
                pageNum: page,
                pageSize: row
            }

            that.$request("/api/customers/getCustomersByParams/1_0", payload, "POST").then((data) => {
                this.dataSource = data.rows
                that.pagePagination['total'] = data.total
                that.pagePagination['pageSize'] = data.row
            })
        },
        queryStageHandle() {
            let that = this
            that.$request("/api/clientStage/queryClientStageByParams/1_0", {}, "POST").then((data) => {
                let dataSource = data.rows
                let stageList = []

                dataSource.map((item) => {
                    stageList.push({
                        ...item,
                        label: item.clientStatusName,
                        value: item.clientStatusId
                    })
                })

                that.clientStageOptions = [...stageList]
            })
        },
        visibleHandle() {
            this.setVisible = !this.setVisible

            if (this.setVisible) {
                this.init()
            }

            this.step = '1'
        },
        queryDataHandle(page, rows) {
            let that = this
            let data = [...that.dataSource]
            that.pagePagination['currentPage'] = page
            that.pagePagination['pageSize'] = rows
            let length = that.dataSource.length
            that.pageDataSource = data.splice((page - 1) * rows, page * rows > length ? length - (page - 1) * rows : rows)
        },
        //转换方法
        formatter(row, column) {
            let field = column.property;
            let value = row[field];
            return this.$utils.formatterForLable(field, value, this.fieldType, this['dictionary'][field]);
        },
        //列表选中方法
        tableRowChange(val) {
            this.multipleSelection = val;
        },
        submitHandle() {
            let clientList = []
            let v = this.step2Data.clientStage
            let o = this.clientStageOptions.filter(item => item.value == v)

            this.multipleSelection.map((item) => {
                clientList.push({
                    ...item,
                    clientStatusId: v
                })
            })

            let payload = {
                clientList,
                clientHistoryRemark: "管理员设置阶段为：" + o[0].label
            }

            this.$request("/api/customers/updateCustomers/1_0", payload, "POST").then((data) => {
                this.$message.success("设置客户标签成功！")
                this.$emit("queryDataHandle", 1, 10)
                this.setVisible = false
            })
        }
    }
}
</script>

<style>

.stage_dialog .el-dialog {
    margin-top: 5vh !important;
}
</style>

<style scoped>
.stage_dialog .el-input {
    width: 200px;
}
</style>
